package com.example;

import com.example.dao.UserDao;
import com.example.dao.UserDaoImpl;
import com.example.entity.SexEnum;
import com.example.entity.User;
import com.example.utils.JdbcUtils;

import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @author zhangs
 * @version v1.0
 * @decription
 * @date 2021/4/3 11:58
 **/
public class Main {
    public static void main(String[] args) {
        try (UserDao userDao = new UserDaoImpl("jdbc.properties");) {
            // 查询单条数据
            User user = userDao.queryById(1);
            System.out.println(user);

            // 查询多条数据
            List<User> users = userDao.queryLikeUsername("cb");
            users.forEach(System.out::println);

            // 查询多条数据
            List<User> users1 = userDao.queryLikeUsernameAndSex("dc", 0);
            users1.forEach(System.out::println);

            // 插入单条数据
            User insert = User.of().setUsername("张大大").setAge(18).setSex(SexEnum.FEMALE);
            int i = userDao.insertOne(insert);
            System.out.println(i);
            System.out.println(insert);

            // 批量插入
            // 生成数据
            Random random = new Random();
            List<User> users2 = new ArrayList<>(1000);
            for (int j = 0; j < 1000; j++) {
                String name = UUID.randomUUID().toString();
                int age = random.nextInt(100);
                SexEnum sex = SexEnum.getSexEnumByCode(random.nextInt(3));

                users2.add(User.of().setUsername(name).setAge(age).setSex(sex));
            }

            // 执行批量操作
            int[] ints = userDao.insertMany(users2);
            int count = 0;
            for (int anInt : ints) {
                if (anInt == 1) {
                    count++;
                }
            }
            System.out.println(count);

            // 更新单条数据
            int j = userDao.updateOne(User.of().setUid(10).setUsername("米兰"));
            System.out.println("单条更新：" + j);

            // 批量更新
            List<User> users3 = new ArrayList<>(1000);
            for (int w = 0; w < 1000; w++) {
                String name = UUID.randomUUID().toString().substring(6);

                users3.add(User.of().setUid(w + 1000).setUsername(name));
            }
            int i1 = userDao.updateMany(users3);
            System.out.println("批量更新成功数：" + i1);

            // 单条删除
            int i2 = userDao.deleteOne(1);
            System.out.println("删除数据" + i2);

            // 批量删除
            List<Integer> uids = new ArrayList<>(1000);
            for (int k = 2000; k < 3000; k++) {
                uids.add(k);
            }
            int i3 = userDao.deleteMany(uids);
            System.out.println("批量删除数：" + i3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
